﻿using UnityEngine;
using System.Collections;

public class EndLevelScript : MonoBehaviour {

	public bool backToMenu = false;

	void Start(){

	}

	void OnTriggerEnter(Collider collision){
		if(collision.gameObject.tag == "Player" && !collision.gameObject.GetComponent<Character>().Dead){
			GameObject player = collision.gameObject;
			player.rigidbody.constraints = RigidbodyConstraints.None;
			player.rigidbody.AddForce(0,0,-1000);
			player.rigidbody.AddTorque(0,0,100);
			StartCoroutine("LevelEnd");
		}
	}

	IEnumerator LevelEnd(){
		yield return new WaitForSeconds(2.0f);
		backToMenu = true;
		Time.timeScale = 0;
	}

	void OnGUI(){
		if(backToMenu == true)
		{
			if (GUI.Button (new Rect(Screen.width/2-100, Screen.height/2-30,200,60), "MainMenu")) 
			{ 
				Time.timeScale = 1;
				Application.LoadLevel("MainMenu");
			}
		}
	}
}
